<?php
/**
 * DATABASE implements two interfaces, DBConsts, and DBFuncs.
 * 
 * DBConsts contains connection-specific parameters.
 * 
 * DBFuncs sets the base list of required functions.
 * 
 * The constructor takes a database name as its only input and performs
 * the connection and database selection procedures.
 *
 * Copyright 2009 Geoff Foley
 * Distributed under the terms of the GNU General Public License
 * http://www.gnu.org/licenses/gpl-3.0.txt
 */

require('dbconsts_int.php');
require('database_int.php');

class database implements DBFuncs {

// Constructor
    public function __construct($db, $host, $user, $password) {
        $this->db_name = $db;

        $this->db_link = mysql_connect($host, $user, $password);
        if (!$this->db_link) die('Could not connect: ' . mysql_error());
		mysql_query('SET NAMES utf8', $this->db_link);

        $this->db_select = mysql_select_db($this->db_name,$this->db_link);
        if(!$this->db_select) die('No database selected: ' . mysql_error());
    }

// Interface Functions
    public function affected_rows() {
        return mysql_affected_rows($this->db_link);
    }

    public function clean_results() {
        if($this->query_id != 0) mysql_freeresult($this->query_id);
    }

    public function close() {
        if($this->db_link != 0) mysql_close($this->db_link);
    }

    public function delete($queryString) {
        $this->query_id = @mysql_query($queryString,$this->db_link);
        if (!$this->query_id) die('Invalid SQL Syntax: ' . mysql_error());
    }

    public function fetch_lengths() {
        return mysql_fetch_lengths($this->query_id);
    }

    public function fetch_object() {
        return mysql_fetch_object($this->query_id);
    }

    public function fetch_array($fetch=0) {
        if($fetch == 0) {
            $result = @mysql_fetch_assoc($this->query_id);
        } else {
            $result = @mysql_fetch_array($this->query_id);
        }

        if(!is_array($result))
            return false;

        foreach($result as $key => $val) {
            $result[$key] = trim($val);
        }
        return $result;
    }

	public function fetch_row()
	{
		$row = @mysql_fetch_array($this->query_id);
		return $row;	
	}
	
	public function getLastInsertedID()
	{
		return mysql_insert_id($this->db_link);
	}
	
	public function total_records()
	{
		return mysql_num_rows($this->query_id);	
	}
	
    public function get_server() {
        return mysql_get_server_info($this->db_link);
    }

    public function max_row($tablename,$field) {
        $sql = "SELECT MAX({$field}) FROM {$tablename}";
        $this->executeQuery($sql);
        $result = @mysql_fetch_array($this->query_id);
        return $result[0];
    }

    public function num_fields() {
        return mysql_num_fields($this->query_id);
    }

    public function num_rows() {
        return mysql_num_rows($this->query_id);
	}
	
    public function setQuery($queryString, $firedBy='open') {
		$this->query = $queryString;
		$this->queryFiredFrom = $firedBy;
	}


    public function executeQuery() {
        $this->query_id = @mysql_query($this->query, $this->db_link);
		
        //if (!$this->query_id) die('Invalid SQL Syntax Inside database::query ('.$queryString.'): ' . mysql_error());
		
		//Hiding the SQL information from sucrity reason
        if (!$this->query_id) 
		{
			//die('Invalid SQL Syntax Inside database');
			$this->mysqlError = mysql_error($this->db_link);
			return false;
		}
		else
		{
			return true;
		}
    }
	
	public function getMysqlError()
	{
		return $this->mysqlError;
	}
}
?>
